package com.gowants.pems.controller;

import com.gowants.pems.aspect.Importance;
import com.gowants.pems.aspect.OperType;
import com.gowants.pems.aspect.ScmsAopLog;
import com.gowants.pems.config.AccessLimit;
import com.gowants.pems.model.SysPermission;
import com.gowants.pems.model.SysPower;
import com.gowants.pems.model.SysUser;
import com.gowants.pems.service.SysPermissionService;
import com.gowants.pems.service.SysPowerService;
import com.gowants.pems.service.SysUserService;
import com.gowants.pems.util.DateUtil;
import com.gowants.pems.util.JSONMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping(value = "/sysPower")
public class SysPowerController extends BaseController {

	@Autowired
	private SysPowerService scmsPowerService;
	@Autowired
	private SysUserService scmsUserService;
	@Autowired
	private SysPermissionService scmsPermissionService;

	
	/**
	 * 获取个人所有权限
	 * 
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/selectPower")
	@AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
	@ScmsAopLog(description = "获取个人所有权限", opType = OperType.execute, importance = Importance.important)
	public JSONMap selectPower(String request, HttpServletRequest req) {
		String code = "";
		try {
			SysPower fp = new SysPower();
			SysUser scmsUser = scmsUserService.selectOne(getUserId());
			if(-1==scmsUser.getType()){
				fp.setIsenable(1);
				List<SysPower> fpmsPower = scmsPowerService.selectAdmin(fp);
				return doResponse(req, code, fpmsPower);
			}
			else
			{
				String role_id="";
				if(null!=scmsUser.getRole())
				{
					role_id=scmsUser.getRole();
				}
				fp.setRoleId(role_id);
				fp.setIsenable(1);
				List<SysPower> fpmsPower = scmsPowerService.select(fp);
				return doResponse(req, code, fpmsPower);
			}
		} catch (Exception e) {
			code = e.getMessage();
			return doResponse(req, code, null);
		}
	}
	
	
	/**
	 * 获取个人权限
	 * 
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/selectPowerbyId")
	@AccessLimit(seconds = 1, maxCount = 10,needLogin=true) //1秒内 允许请求10次
	@ScmsAopLog(description = "获取个人权限", opType = OperType.execute, importance = Importance.important)
	public JSONMap selectPowerbyId(String request, HttpServletRequest req) {
		String code = "";
		try {
			JSONMap requestBody = getRequestBody(request);
			String permissionId = requestBody.getString("permissionId");
			SysPower fp = new SysPower();
			SysUser scmsUser = scmsUserService.selectOne(getUserId());
			//fp.setUserId(getUserId());
			fp.setRoleId(scmsUser.getRole());
			fp.setIsenable(1);
			fp.setType(1);
			fp.setPid(permissionId);
			List<SysPower> fpmsPower = scmsPowerService.select(fp);
			return doResponse(req, code, fpmsPower);
		} catch (Exception e) {
			code = e.getMessage();
			return doResponse(req, code, null);
		}
	}

	/**
	 * 批量处理用户权限
	 * 
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/saveUserPower")
	@AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
	public JSONMap saveUserPower(String request, HttpServletRequest req) {
		String code = "";
		try {
			JSONMap requestBody = getRequestBody(request);
			String userId = requestBody.getString("userId");
			String userType = requestBody.getString("userType");
			SysUser record = new SysUser();
			record.setType(Integer.parseInt(userType));// 社区管理员
			//record.setStreetCode("150105006");
			List<SysUser> UserList = this.scmsUserService.select(record);
			System.out.println("总共" + UserList.size());
			int j = 0;
			int i = 0;
			for (SysUser u : UserList) {
				if(!u.getId().equals(userId))
				{
					scmsPowerService.deleteByRoleId(u.getId());
					System.out.println("处理" + u.getUserName());
					SysPower fp0 = new SysPower();
					//fp0.setUserId(userId);
					fp0.setIsenable(1);
					List<SysPower> scmsPower0 = scmsPowerService.select(fp0);
					j = UserList.size() * scmsPower0.size();
					for (SysPower fp1 : scmsPower0) {
					    SysPower fp = new SysPower();
						//fp.setUserId(u.getId());
						fp.setPermissionId(fp1.getPermissionId());
						fp.setId(UUID.randomUUID().toString());
						fp.setCreateTime(DateUtil.getDate());
						fp.setCreateUid(record.getCreateUid());
						i = i + scmsPowerService.save(fp);
						System.out.println("处理" + i + "条");
	
					}
				}
			}

			if (i == j) {
				System.out.println("处理成功");
			} else {
				System.out.println("处理失败:总共" + j + ",完成:" + i);
			}
			return doResponse(req, code, null);
		} catch (Exception e) {
			code = e.getMessage();
			return doResponse(req, code, null);
		}
	}

	/**
	 * 批量处理用户权限
	 * 
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/saveUserPowerAdmin")
	@AccessLimit(seconds = 1, maxCount = 3,needLogin=true) //1秒内 允许请求3次
	public JSONMap saveUserPowerAdmin(String request, HttpServletRequest req) {
		SysPermission record = new SysPermission();
		List<SysPermission> list = scmsPermissionService.select(record);
		for (SysPermission l : list) {
			SysPower fp = new SysPower();
			//fp.setUserId("1");
			fp.setPermissionId(l.getId());
			fp.setId(UUID.randomUUID().toString());
			fp.setCreateTime(DateUtil.getDate());
			fp.setCreateUid("1");
			scmsPowerService.save(fp);
		}
		return doResponse(req, "", null);

	}
}
